package com.engine.hikvision.cron.door;

import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.BaseBean;
import weaver.interfaces.schedule.BaseCronJob;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @version 1.0
 * @Title ecology-9
 * @Company 泛微软件
 * @CreateDate 2024/10/25
 * @Description 统计食堂三餐数据汇总
 * @Author AdminZm
 */
public class CountCanteenDataJob extends BaseCronJob {

    BaseBean baseBean = new BaseBean();

    @Override
    public void execute() {
        baseBean.writeLog("CountCanteenDataJob start.");
        try {
            RecordSet rs = new RecordSet();
            RecordSet rs1 = new RecordSet();
            SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm:ss");
            String querySql = "select b.*, c.lastname, c.workcode, c.departmentid, c.SUBCOMPANYID1 from (" +
                    "  select a.xm, a.ny, sum(CAST(a.zccs AS INT)) as allZccs, sum(CAST(a.wccss AS INT)) as allWccss, " +
                    "     sum(CAST(a.wccs AS INT)) as allWccs, sum(CAST(a.xycs AS INT)) as allXycs FROM \n" +
                    "     (select xm, FORMAT(CAST(CONVERT(DATE, dkrq) AS DATE), 'yyyy-MM') AS ny, zccs, wccss, wccs, xycs from uf_stdkxx WHERE dkrq is not null) a group by a.xm, a.ny " +
                    " ) b left join hrmresource c on b.xm = c.id";
            rs.execute(querySql);
            while (rs.next()) {
                String xm = rs.getString("xm");
                String ny = rs.getString("ny");
                if (StringUtils.isEmpty(xm) ||StringUtils.isEmpty(ny)) {
                    continue;
                }
                String allZccs = rs.getString("allZccs");
                String allWccss = rs.getString("allWccss");
                String allWccs = rs.getString("allWccs");
                String allXycs = rs.getString("allXycs");
                String workcode = rs.getString("workcode");
                String bm = rs.getString("departmentid");
                String fb = rs.getString("SUBCOMPANYID1");
                String lastname = rs.getString("lastname");

                String nowDate = sdfDate.format(new Date());
                String nowTime = sdfTime.format(new Date());

                int id = 0;
                rs1.executeQuery("SELECT id FROM uf_stsccshzxx WHERE yg = ? AND yf = ?", xm, ny);
                if (rs1.next()) {
                    id = rs1.getInt("id");
                }
                if (id > 0) {
                    rs1.executeUpdate("UPDATE uf_stsccshzxx SET bm = ?, fb = ?, zccs = ?, wccs = ?, " +
                            "ywccs = ?, xycs = ? WHERE id = ?", bm, fb, allZccs, allWccss, allWccs, allXycs, id);
                } else {
                    rs1.executeUpdate("INSERT INTO uf_stsccshzxx (yg, ygxm, gh, bm, fb, yf, zccs, wccs, ywccs, xycs, " +
                            "formmodeid, MODEUUID, modedatacreater,modedatacreatertype, modedatacreatedate, " +
                            "modedatacreatetime) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", xm, lastname, workcode, bm, fb,
                            ny, allZccs, allWccss, allWccs, allXycs, "17", UUID.randomUUID().toString(), "1", "0", nowDate, nowTime);
                }
            }

            // 权限重构
            ModeRightInfo modeRightInfo = new ModeRightInfo();
            modeRightInfo.setNewRight(true);
            modeRightInfo.editModeDataShare(1, 17, 24);
        } catch (Exception e) {
            baseBean.writeLog("CountCanteenDataJob error:" + e);
        }
        baseBean.writeLog("CountCanteenDataJob end.");
    }
}
